package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.SplitSwitch;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kyx on 2018/6/13.
 */

public class SplitSwitchDao {
    private Context mContext;
    private Dao<SplitSwitch, Integer> splitSwitchDaoOpe;
    private DatabaseHelper helper;

    public SplitSwitchDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            splitSwitchDaoOpe = helper.getDao(SplitSwitch.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     *
     */
    public void add(SplitSwitch splitSwitch) {
        try {
            splitSwitch.setChecked(false);
            splitSwitchDaoOpe.create(splitSwitch);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public int updateSplitSwitch(SplitSwitch splitSwitch) {
        try {
            splitSwitch.setChecked(false);
            return splitSwitchDaoOpe.update(splitSwitch);
            //    LogHelper.i("updateLightFannumber" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public SplitSwitch get(int id) {
        try {
            return splitSwitchDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public SplitSwitch getByUuid(String uuid) {
        List<SplitSwitch> splitSwitchList = null;
        try {
            splitSwitchList = splitSwitchDaoOpe.queryBuilder().where().eq
                    ("uuid", uuid).query();
            if (splitSwitchList != null && splitSwitchList.size() > 0) {
                return splitSwitchList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     */
    public List<SplitSwitch> getAllSplitSwitch() {
        List<SplitSwitch> splitSwitchList = new ArrayList<>();
        try {
            splitSwitchList = splitSwitchDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return splitSwitchList;
    }

    public SplitSwitch getById(String groupId,int category) {
        List<SplitSwitch> splitSwitchList = null;
        try {
            splitSwitchList = splitSwitchDaoOpe.queryBuilder().where().eq
                    ("groupId", groupId).and().eq
                    ("category", category).query();
            if (splitSwitchList != null && splitSwitchList.size() > 0) {
                return splitSwitchList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<SplitSwitch> getSplitSwitchs(String familyId,int category) {
        List<SplitSwitch> splitSwitchList = new ArrayList<>();
        try {
            splitSwitchList = splitSwitchDaoOpe.queryBuilder().where().eq ("familyId", familyId).and().eq
                    ("category", category).query();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return splitSwitchList;
    }

    public int delete(SplitSwitch splitSwitch) {
        try {
            return splitSwitchDaoOpe.delete(splitSwitch);
            //    LogHelper.i("deleLightFannumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void deleteAll() {
        try {
            int number = splitSwitchDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<SplitSwitch> splitSwitchList) {
        try {
            int number = splitSwitchDaoOpe.delete(splitSwitchList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void deleteFamilySplitSwitchs(String familyId) {
        try {
            List<SplitSwitch> splitSwitchList = getSplitSwitchs(familyId,11);
            int number = splitSwitchDaoOpe.delete(splitSwitchList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public SplitSwitch getSplitSwitchByName(String familyId, String name,int category) {
        try {
            List<SplitSwitch> splitSwitchList = splitSwitchDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).and().eq
                    ("name", name).and().eq
                    ("category", category).query();
            if (splitSwitchList != null && splitSwitchList.size() > 0)
                return splitSwitchList.get(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
